package com.atguigu.web;

import com.atguigu.pojo.User;
import com.atguigu.service.UserService;
import com.atguigu.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegistServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();//本文件是web层,通过serice层,调用dao层,控制数据库
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //System.out.println(req.toString());//打印不出来,如何打印req内部信息
        String username=req.getParameter("username");
        String password=req.getParameter("password");
        String email=req.getParameter("email");
        String code=req.getParameter("code");//验证码

        req.setAttribute("username",username);
        req.setAttribute("password",password);
        req.setAttribute("email",email);
        req.setAttribute("code",code);

        if(code.equalsIgnoreCase("abcde")){//验证码写死了,以后写活它
            //equalsIgnoreCase忽略大小写 别再用==了,low
            if(userService.existUsername(username)){
                System.out.println(username+"已存在");
                req.setAttribute("msg",username+"已存在");
                req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);//用户名已存在,所以需要用户用新用户名注册,于是让浏览器跳回注册页面
            }else{
                userService.registUser(new User(null,username,password,email));
                req.getRequestDispatcher("/pages/user/regist_success.jsp").forward(req,resp);
            }
        }else {
            req.setAttribute("msg","验证码错误");
            req.getRequestDispatcher("/pages/user/regist.jsp").forward(req,resp);//让用户重新注册,于是让浏览器返回一个注册页面
            //req.getRequestDispatcher()方法,是让浏览器跳回一个页面,一个静态页面.这个路径的出发点是项目的web目录

        }
    }
}
